﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.Actions.Parameters;
using RibbonEditor.AppCode;
using RibbonEditor.Forms;
using System.Reflection;

namespace RibbonEditor.Actions
{
    public class Url : IAction, IXmlBuildable
    {
        XmlNode urlXmlNode;

        public string Address { get; set; }
        public bool PassParams { get; set; }
        public Enumerations.WinMode WinMode { get; set; }
        public string WinParams { get; set; }
        public List<IParameter> Parameters { get; set; }

        public Url()
        {
            this.Parameters = new List<IParameter>();
        }

        public Url(XmlNode node):this()
        {
            try
            {
                this.urlXmlNode = node;

                if (XmlHelper.GetAttribute(node, "Address").Length > 0)
                {
                    this.Address = XmlHelper.GetAttribute(node, "Address");
                }

                if (XmlHelper.GetAttribute(node, "PassParams").Length > 0)
                {
                    this.PassParams = XmlHelper.GetAttribute(node, "PassParams") == "true";
                }

                if (XmlHelper.GetAttribute(node, "WinMode").Length > 0)
                {
                    this.WinMode = (Enumerations.WinMode)Enum.Parse(typeof(Enumerations.WinMode), XmlHelper.GetAttribute(node, "WinMode"));
                }

                if (XmlHelper.GetAttribute(node, "WinParams").Length > 0)
                {
                    this.WinParams = XmlHelper.GetAttribute(node, "WinParams");
                }

                foreach (XmlNode parameterNode in node.ChildNodes)
                {
                    if (parameterNode.NodeType != XmlNodeType.Comment)
                    {
                        IParameter param = ParameterHelper.GetIParameterFromXml(parameterNode);
                        this.Parameters.Add(param);
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IAction Members

        public void AddNode(XmlNode actionsNode)
        {
            try
            {
                XmlNode urlNode = XmlHelper.AddNode(actionsNode, "Url");
                XmlHelper.AddAttribute(urlNode, "Address", this.Address);
                XmlHelper.AddAttribute(urlNode, "PassParams", this.PassParams);
                XmlHelper.AddAttribute(urlNode, "WinMode", this.WinMode);
                XmlHelper.AddAttribute(urlNode, "WinParams", this.WinParams);

                foreach (IParameter parameter in this.Parameters)
                {
                    parameter.AddNode(urlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode urlNode = new TreeNode("Url");
                urlNode.ContextMenuStrip = this.BuildContextMenu(urlNode);
                urlNode.Tag = this;

                foreach (IParameter param in this.Parameters)
                {
                    param.BuildTreeViewElement(urlNode);
                }

                parentNode.Nodes.Add(urlNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode javascriptFunctionNode)
        {
            ToolStripMenuItem tsmi1 = new ToolStripMenuItem("Add Boolean Parameter");
            ToolStripMenuItem tsmi2 = new ToolStripMenuItem("Add Crm Parameter");
            ToolStripMenuItem tsmi3 = new ToolStripMenuItem("Add Decimal Parameter");
            ToolStripMenuItem tsmi4 = new ToolStripMenuItem("Add Integer Parameter");
            ToolStripMenuItem tsmi5 = new ToolStripMenuItem("Add String Parameter");
            tsmi1.Tag = "BoolParameter";
            tsmi2.Tag = "CrmParameter";
            tsmi3.Tag = "DecimalParameter";
            tsmi4.Tag = "IntParameter";
            tsmi5.Tag = "StringParameter";
            ToolStripMenuItem tsmdx = new ToolStripMenuItem("Display Xml");
            ToolStripMenuItem tsmd = new ToolStripMenuItem("Delete");

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = javascriptFunctionNode;
            cms.Items.Add(tsmi1);
            cms.Items.Add(tsmi2);
            cms.Items.Add(tsmi3);
            cms.Items.Add(tsmi4);
            cms.Items.Add(tsmi5);
            cms.Items.Add(new ToolStripSeparator());
            cms.Items.Add(tsmdx);
            cms.Items.Add(new ToolStripSeparator());
            cms.Items.Add(tsmd);

            return cms;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

                switch (e.ClickedItem.Text)
                {
                    case "Display Xml":
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml("<root></root>");

                            ((IXmlBuildable)selectedNode.Tag).AddNode(doc.DocumentElement);

                            XmlContentDisplayDialog dialog = new XmlContentDisplayDialog(doc.DocumentElement.InnerXml);
                            dialog.StartPosition = FormStartPosition.CenterParent;
                            dialog.ShowDialog();
                        }
                        break;
                    case "Delete":
                        {
                            selectedNode.TreeView.SelectedNode = selectedNode.Parent;
                            UIActionHelper.Delete(selectedNode);
                        }
                        break;
                    default:
                        {
                            string itemTag = e.ClickedItem.Tag.ToString();

                            Assembly assembly = Assembly.GetExecutingAssembly();
                            AssemblyName assemblyName = assembly.GetName();
                            Type t = Type.GetType(assemblyName.Name + ".Actions.Parameters." + itemTag);

                            IParameter param = (IParameter)Activator.CreateInstance(t,new object[]{true});

                            this.Parameters.Add(param);
                            param.BuildTreeViewElement(selectedNode);
                        }
                        break;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}